/**
 * Copyright (c) 2017-present, Liu Jinyong
 * All rights reserved.
 *
 * https://github.com/huanxsd/MeiTuan
 * @flow
 */

import api from './api'


import React, { Component } from 'react';
class NetUitl extends React.Component{
    /*
     *  get请求
     *  url:请求地址
     *  data:参数
     *  callback:回调函数
     * */
    static get(url,params,callback){
        var totalUrl=api.baseUrl+url
        if (params) {
            let paramsArray = [];
            //拼接参数
            Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
            if (url.search(/\?/) === -1) {
                url += '?' + paramsArray.join('&')
            } else {
                url += '&' + paramsArray.join('&')
            }
        }
        //fetch请求
        fetch(totalUrl,{
            method: 'GET',
        })
            .then((response) => {
                callback(response)
            }).done();
    }
    /*
     *  post请求
     *  url:请求地址
     *  data:参数
     *  callback:回调函数
    //  * */
    // let headers = new FormData();
    //     headers.append("x-api-version", "2.2"),
    //         headers.append("x-mdi-sess", 'MTEwMzQx-PiFYqzZFGdjUD6hqsNA3OMjZon03gxKh5yfn2ubv8QcccKsUvkwyqug8t4qC8MFh'),
    //         headers.append("x-client", "web"),
    //         headers.append("User-Agent", "web"),
    //         headers.append("Content-type", "application/json")
    static post(url,params,callback){
        //fetch请求
        var totalUrl=api.baseUrl+url
        fetch(totalUrl,{
            method: 'POST',
            headers:{
                'x-api-version': 2.2,
                'x-mdi-sess': 'MTEwMzQx-PiFYqzZFGdjUD6hqsNA3OMjZon03gxKh5yfn2ubv8QcccKsUvkwyqug8t4qC8MFh',
                'x-client': 'react-native',
                'User-Agent':'react-native',
                'Content-type': 'application/json'
            },
            body:JSON.stringify(params)
        })
            .then((response) => response.json())
            .then((responseJSON) => {
                callback(responseJSON)
            }) .done();
    }



}

module.exports = NetUitl;
